<?php
namespace app\backend\controller;
use think\Db;
use \think\Validate;
use think\Controller;
class Admin extends Controller
{
	public function mem_list()
    {
    	$ret=db('user')->select();
    	$this->assign('ret',$ret);
      return view('admin/mem_list');
    }

    public function mem_delete()
    {
        $user_id=input('get.user_id');
    if($user_id!="")
    {
      $ret=db('user')->where("user_id='{$user_id}'")->delete();
      if($ret==false)
         {
            $this->error('删除失败');
          }
             $this->success('删除成功');

    }
    }


	public function admin_list()
    {
    	$ret=db('admin')->select();
      for($i=0;$i<count($ret);$i++)
      {
        if($ret[$i]['admin_logintime']=='')
        {
          $ret[$i]['admin_logintime']='1546272000';
        }
      /*else
      {
        $ret[$i]['admin_logintime']='$val.admin_logintime|date="Y-m-d",###';}
       foreach ($ret as $val) {
        if($val['admin_logintime']='')
         {
           $ret['admin_logintime']='1546272000';
           
         }
         
       }
       }*/
       //dump($ret);
       }
      
    	$this->assign('ret',$ret);
      return view('admin/admin_list');
    }
	public function admin_add()
    {
        return view('admin/admin_add');
    }
    public function admin_changepwd()
    {
    	return view('admin/admin_changepwd');
    }
    public function changepwd()
    {
    	if(request()->isPost())
       {
        
        $admin_origin_password = input('post.admin-old-password');
        $admin_new_password = input('post.admin-new-password');
        $admin_confirm_password = input('post.admin-confirm-password');
        //dump($_POST);die;
        
        $rule = [
            'admin_origin_password'  => 'require',
            'admin_new_password' => 'require|min:6|max:30',
            'admin_confirm_password' => 'require|confirm:admin_new_password'
        ];
        $msg = [
            'admin_origin_password.require'  => '修改密码前请输入原密码！',
            'admin_new_password.require' => '请输入新密码',
            'admin_new_password.min' => '密码长度最小为6位',
            'admin_new_password.max' => '密码长度最多为30位',
            'admin_confirm_password.require'  => '请确认密码！',
            'admin_repwd.confirm' => '两次密码不一致'
            
        ];

        $data = [
            'admin_origin_password' => $admin_origin_password,
            'admin_new_password'    =>  $admin_new_password,
            'admin_confirm_password'  =>  $admin_confirm_password   
        ];
        $validate = new Validate($rule, $msg);
        $result   = $validate->check($data);
        if (!$result ) {
            $this->error($validate->getError());
        }

       /*控制登录密码必须有数字和字母组成 */
        if(preg_match("/[A-Za-z]/",$admin_new_password)&preg_match("/\d/",$admin_new_password))
        {}
        else
        {
            $this->error("密码必须由数字和字母组成");
         }
         

         $data = [  
            'admin_password' =>  md5($admin_new_password)  
        ];

        $admin_id = input('session.admin_id');
       
        
        
        $admin_info = db('admin')->where('admin_id', $admin_id) ->value('admin_password');
         
         if($admin_info != md5($admin_origin_password))
         {
            $this->error("您输入的原密码不正确，请重新输入");
         }else
         {
             $ret = db('admin') ->where('admin_id', $admin_id) ->update($data);
             if(!$ret){
                $this->success("密码修改失败！");
             }
             else
             {
                $this->success("密码修改成功,请使用新密码重新登录",url('/backend/login/login'));

             }

         }

       }
    }

    public function admin_insert()
    {
        if ( request()->isPost()) 
        {
          
            $admin_name=input('post.admin_name');
            $admin_phone = input('post.admin_phone');
            $admin_pwd = input('post.admin_pwd');
            $admin_repwd = input('post.password1');
            $verify=input('verify');
            $rule = [
	                'admin_name'  => 'require|unique:admin,admin_name',
	                'admin_phone' => 'require|unique:admin,admin_phone',
	                'admin_pwd'   => 'require|min:6',
	                'admin_repwd' => 'require|confirm:admin_pwd'
		            ];
            $msg = [
	                'admin_name.require' => '请输入姓名',   
	                'admin_name.unique' => '此姓名已存在',

	                'admin_phone.require' => '请填写手机号',
	                
	                'admin_phone.unique'  => '此手机号已被注册，请使用其他手机号！',

	                'admin_pwd.require'  => '请设置登录密码！',
	                'admin_pwd.min' => '密码长度最小为6位',

	                'admin_repwd.require'  => '请确认密码！',
	                'admin_repwd.confirm' => '两次密码不一致'
                
		            ];
        
            if(preg_match("/^\d*$/", $admin_pwd))
            
                $this->error("密码必须包含字母");//全数字
            
            if(preg_match("/^[a-z]*$/i",$admin_pwd))
            
                $this->error( "密码必须包含数字");//全字母
            
            if(!preg_match("/^[a-z\d]*$/i",$admin_pwd))
            
                $this->error("密码只能包含数字和字母");//有数字有字母 ";
           /* if(empty($verify))
            {
                $this->error("验证码不能为空");
            }
            else if(!captcha_check($verify))
            {
                $this->error("验证码错误");
            }*/
            
          
            $data = [
	                'admin_name'   =>  $admin_name,
	                'admin_phone'  =>  $admin_phone,
	                'admin_pwd'    =>  $admin_pwd,
	                'admin_repwd'  =>  $admin_repwd
                
		            ];

            $validate = new Validate($rule, $msg);
            $result   = $validate->check($data);
          
            if (!$result ) {
                $this->error($validate->getError());
            }

            $data = [
	                'admin_name'   =>  $admin_name,
	                'admin_phone'  =>  $admin_phone,
	                'admin_pwd' =>  md5($admin_pwd),
                    'admin_createtime' =>  time()
		            ];
            $ret = db('admin')->insert($data);
            if ( $ret == false ) {
                $this->error('添加失败');
            } 
            
            $this->success("添加成功！",Url('/backend/admin/admin_list'));
            
        }

    }

     public function admin_delete()
    
    {
      $admin_id=input('get.admin_id');
    if($admin_id!="")
    {
      $ret=db('admin')->where("admin_id='{$admin_id}'")->delete();
      if($ret==false)
         {
            $this->error('删除失败');
          }
             $this->success('删除成功');

    }
    } 

}